package iks.utils 
{
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	import flash.text.*;
	import flash.utils.*;
	
	/**
	 * 查看当前的状态, FPS, 内存信息
	 * @author StefanieKaka
	 */
	public final class FPSCounter extends Sprite 
	{
		
		public function FPSCounter() 
		{
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStageHandler);
			this.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
		
		private function init() : void
		{
			m_timer = new Timer(1000, 0);
			m_timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
			m_timer.start();
			
			m_tick = 0;
			
			m_text = new TextField();
			m_text.selectable = false;
			m_text.autoSize = TextFieldAutoSize.LEFT;
			m_text.textColor = 0xffffff;
			m_text.background = true;
			m_text.backgroundColor = 0x808080;
			updateText(this.stage.frameRate);
			this.addChild(m_text);
		}
		
		private function onAddToStageHandler(evt : Event) : void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddToStageHandler);
			init();
		}
		
		private function onTimerHandler(evt : TimerEvent) : void
		{
			updateText(m_tick);
			m_tick = 0;
		}
		
		private function onEnterFrameHandler(evt : Event) : void
		{
			++m_tick;
		}
		
		private function updateText(frame : uint) : void
		{
			m_text.text = "FrameRate: " + frame + "/" + this.stage.frameRate + "\n";
			m_text.appendText("Memory: " + int(System.totalMemory / 1024 / 1024) + "MB");
		}
		
		private var m_tick : uint = 0;
		private var m_text : TextField = null;
		private var m_timer : Timer = null;
	}

}